package com.manager.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileUtil {
	// 默认读写文件时的编码格式
	private static String defaultEncoding = "UTF-8";

	/**
	 * isOverWrite 如果文件存在，是否重写文件
	 * @param0 absFilePath
	 * @param1 content
	 * @param2 encoding 
	 * 注：只用于创建文件
	 * (1)只有absFilePath，则只创建一个空文件
	 * (2)有absFilePath，content，则默认以UTF-8编码创建文件
	 * (3)有absFilePath，content，encoding，则以指定编码创建文件
	 */
	public static boolean createFile(boolean isOverWrite, String... params) {
		String encoding = defaultEncoding;
		if (params.length == 0) {
			return false;
		}

		if (params.length == 3) {
			encoding = params[2];
		}

		File file = new File(params[0]);
		FileOutputStream fops = null;

		try {
			if (!file.exists()) {// 如果该文件不存在，则新建并写入
				file.createNewFile();
				if (params.length > 1 && params[1].length() > 0) {// 如果有要写入的内容，且不为空时
					fops = new FileOutputStream(file);
					fops.write(params[1].getBytes(encoding));
				}
			} else {// 如果该文件已存
				if (isOverWrite) {//直接重写文件
					fops = new FileOutputStream(file);
				} else {//以追加方式在末尾写入
					fops = new FileOutputStream(file, true);
				}
				if (params.length > 1 && params[1].length() > 0) {
					fops.write(params[1].getBytes(encoding));
				}
			}
			
			fops.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * @param0 文件路径
	 * @param1 编码格式，默认以UTF-8编码
	 */
	public static String readFile(String ... params){
		if(params.length == 0){
			return "paramsLength:0，please check your params";
		}
		String encoding = defaultEncoding;
		if(params.length == 2){
			encoding = params[1];
		}
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		StringBuffer sb = null;
		
		try{
			fis = new FileInputStream(params[0]);
			isr = new InputStreamReader(fis, encoding);
			br = new BufferedReader(isr);
			sb = new StringBuffer();
			String data = "";
			while((data = br.readLine()) != null){
				sb.append(data);
			}
			
			br.close();
			isr.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return sb.toString();
	}
	
	/**
	 * 创建一个文件夹
	 * @param folderPath
	 */
	public static boolean createFolder(String folderPath) {
		File file = new File(folderPath);
		if (!file.exists()) {
			file.mkdir();
		}
		return true;
	}
	
	/**
	 * 删除一个文件
	 * @param filePath
	 */
	public static boolean deleteFile(String filePath) {
		File file = new File(filePath);
		if (file.exists() && file.isFile()){
			file.delete();
		}
		return true;
	}
	
	/**
	 * 删除该文件夹，并删除所有子文件夹和子文件
	 * @param folderPath
	 */
	public static boolean deleteFolder(String folderPath) {
		File dir = new File(folderPath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteFolder(folderPath + "/" + files[i].getName());
				} else {
					files[i].delete();
				}
			}
			dir.delete();
		}
		return true;
	}
	
	/**
	 * 保留该文件夹，但删除所有子文件夹和子文件
	 * @param folderPath
	 */
	public static boolean clearFolder(String folderPath) {
		File dir = new File(folderPath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteFolder(folderPath + "/" + files[i].getName());
				} else {
					files[i].delete();
				}
			}
		}
		return true;
	}
	
	/**
	 * 保留该文件夹和其下的所有直接或间接子文件夹，但删除所有直接和间接的子文件
	 * @param folderPath
	 */
	public static boolean clearFolderFiles(String folderPath) {
		File dir = new File(folderPath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					clearFolderFiles(folderPath + "/" + files[i].getName());
				} else {
					files[i].delete();
				}
			}
		}
		return true;
	}
	
	/**
	 * 取得文件的后缀名
	 * @param fileFullName: test.txt or E:/test/test.txt
	 * @return .txt
	 */
	public static String getSuffix(String fileFullName){
		return fileFullName.substring(fileFullName.lastIndexOf("."));
	}
	
	/**
	 * 取得文件的前缀名
	 * @param fileFullName: test.txt or E:/test/test.txt
	 * @return test
	 */
	public static String getPrefix(String fileFullName){
		return fileFullName.substring(fileFullName.lastIndexOf("/") + 1, fileFullName.lastIndexOf("."));
	}
	
	public static void main(String[] args) {
		FileUtil.clearFolderFiles("G:\\tt");
	}
}